package com.example.finalapplication;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.fragment.app.Fragment;


public class ThirdFragment extends Fragment implements View.OnClickListener {
    private static final String TAG = "ThirdFragment";
    private SharedPreferences shared;
    TextView tText;
    EditText input;
    private String text;
    public ThirdFragment(String text){
        this.text = text;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_third, container, false);
        input = (EditText)view.findViewById(R.id.motto);
        tText = view.findViewById(R.id.t_text);
        tText.setText(text);
        shared = getActivity().getSharedPreferences("ThirdInfo", Activity.MODE_PRIVATE);
        String motto1 = shared.getString("motto", "");
        input.setText(motto1);
        view.findViewById(R.id.btn_save).setOnClickListener(this);
        view.findViewById(R.id.btn_log).setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_save:
                String inputStr = input.getText().toString();
                shared.edit().putString("motto",inputStr).apply();
                break;
            case R.id.btn_log:
                Intent intent = new Intent(getActivity(), LogActivity.class);
                startActivity(intent);
                break;
        }
    }
}